package com.runshu.newsmartark.dbutil;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.runshu.newsmartark.base.MyApplication;
import com.runshu.newsmartark.entity.CategoryListEntity;
import com.runshu.newsmartark.entity.ConfirmStockEntity;
import com.runshu.newsmartark.entity.ProductsBean;
import com.runshu.newsmartark.entity.UserInfoEntity;
import com.runshu.newsmartark.util.common.LogUtil;

import java.util.List;

/**
 * Demo class
 *
 * @author yu
 * @date 12/9/20
 * @decs：
 */

public class DBUtils {
    private static DBUtils instance;
    //操作数据库
    SmartRoomDatabase database = SmartRoomDatabase.getInstance( MyApplication.getApplication() );
    UserDao userDao = database.userDao();
    ProAllDao proAllDao = database.proAllDao();
    public static DBUtils getInstance() {
        if (instance == null) {
            synchronized (DBUtils.class) {
                if (instance == null) {
                    instance = new DBUtils();
                }
            }
        }
        return instance;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public ProAllDao getProAllDao() {
        return proAllDao;
    }

     public List<ProductsBean> getproByType(String category, String modle, String manufacturer) {
        return proAllDao.getProByType( category, modle, manufacturer );
    }

    public List<ProAllEntity> getProBySerialNos(String... serialNos) {
        return proAllDao.getProBySerialNos( serialNos );
    }

    //查询本柜中拿出的数据
    public List<ConfirmStockEntity.AllProductsBean> getMenusPros(String... serialNos) {
        return proAllDao.getMenusPros( serialNos );
    }
    //查询本柜中拿出的数据
    public List<ConfirmStockEntity.AllProductsBean> getAddPros(String... serialNos) {
        return proAllDao.getAddPros( serialNos );
    }

    //获取用户全部数据
    public List<UserInfoEntity> getUserList() {
        return userDao.getUserList();
    }


    //返回匹配卡片 用户信息
    public UserInfoEntity getUserByIdCardNo(String cardID) {
        return userDao.getUserByIdCardNo( cardID );
    }

    //返回匹配姓名 用户信息
    public UserInfoEntity getUserByUserName(String userName) {
        return userDao.getUserByUserName( userName );
    }

    //插入用户集合
    public void insertUserList(List<UserInfoEntity> userList) {

        userDao.addUserInfo( userList );

    }
    //插入单体用户数据

    public void insertUserInfo(UserInfoEntity userInfoEntity) {
        userDao.addUserInfo( userInfoEntity );
    }

    //删除用户表数据
    public void deleteAllUserInfo() {
        userDao.deleteAllUserInfo();

    }


    /**
     * 查询最后一条数据的更新时间
     *
     * @return
     */

    String getLastUpdateTime() {
        return proAllDao.getLastUpdateTime();

    }

    //插入医院下所有商品集合
    public void insertAllProList(List<ProAllEntity> proAllList) {

        proAllDao.insertAllPro( proAllList );

    }

    //插入医院下所有商品集合
    public void insertSelfProList(List<ProAllEntity> proAllList) {
        Gson gson = new Gson();
        List<ProSelfEntity> list = gson.fromJson( gson.toJson( proAllList ), new TypeToken<List<ProSelfEntity>>() {
        }.getType() );
        proAllDao.deleteSelfPro();
        proAllDao.insertSelfPro( list );

    }

    //清空资源
    public void release() {
        if (database != null) {
            database.close();
            database.release();
            database = null;
            instance = null;
        }
    }


}